import { createRouter, createWebHistory } from 'vue-router'
import Home from '@/views/Home.vue'
import MomentsPage from '@/views/MomentsPage.vue'
import QuotesPage from '@/views/QuotesPage.vue'
import SceneriesPage from '@/views/SceneriesPage.vue'
import AboutPage from '@/views/AboutPage.vue'
import ContributePage from '@/views/ContributePage.vue'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    meta: {
      title: 'Life 拾光 | 漫步在生活的诗意里'
    }
  },
  {
    path: '/moments',
    name: 'Moments',
    component: MomentsPage,
    meta: {
      title: '生活瞬间 | Life 拾光'
    }
  },
  {
    path: '/quotes',
    name: 'Quotes', 
    component: QuotesPage,
    meta: {
      title: '美好文字 | Life 拾光'
    }
  },
  {
    path: '/sceneries',
    name: 'Sceneries',
    component: SceneriesPage,
    meta: {
      title: '沿途风景 | Life 拾光'
    }
  },
  {
    path: '/about',
    name: 'About',
    component: AboutPage,
    meta: {
      title: '关于十一 | Life 拾光'
    }
  },
  {
    path: '/contribute',
    name: 'Contribute',
    component: ContributePage,
    meta: {
      title: '投稿分享 | Life 拾光'
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  }
})

// 更新页面标题
router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
})

export default router